#!/usr/bin/ python3
# coding=utf-8                                  
# File Name: rpc_client.py                      
# Author: Bill Zhang                            
# Mail: zchcandid@gmail.com                     
# Created Time: Sat 08 Mar 2014 06:15:42 AM CST
import pika

connection = pika.BlockingConnection(pika.ConnectionParameters(
        host='192.168.100.204'))

channel = connection.channel()

channel.queue_declare(queue='rpc_queue')
def to_update(id):
"""to_update is for get the URL and update the price and quantity
   the return value is the json data

"""

def on_request(ch, method, props, body):
    id = int(body)
    print (" [.] ID(%s)"  % (id,))
    response = to_update(id)
    ch.basic_publish(exchange='',
                     routing_key=props.reply_to,
                     properties=pika.BasicProperties(correlation_id = \
                                                         props.correlation_id),
                     body=str(response))
    ch.basic_ack(delivery_tag = method.delivery_tag)

channel.basic_qos(prefetch_count=1)
channel.basic_consume(on_request, queue='rpc_queue')

print (" [x] Awaiting RPC requests to update the hqchip data!")
channel.start_consuming()
